package com.ytzl.itrip.controller.userinfo;

import com.ytzl.itrip.enuminfo.ItripUserErrorCodeEnum;
import com.ytzl.itrip.pojo.po.userinfo.ItripUser;
import com.ytzl.itrip.pojo.vo.userinfo.ItripUserVO;
import com.ytzl.itrip.service.userinfo.ItripUserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import com.ytzl.itrip.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Pattern;

/**
 * Description: 本类用于处理注册相关的业务
 * Created by gengshu on 2019/12/20 11:21
 */
@RestController
@Slf4j
@RequestMapping("/auth/api")
@Api(description = "用于处理注册相关的业务")
public class RegisterController {
//    @GetMapping("/auth/api/ckusr")
//    public ResultUtil checkUser(@RequestParam String name){
//        String names="注册成功";
//        return ResultUtil.success(names);
//    }
    @Autowired
    private ItripUserService itripUserService;
    /**
     *  检测用户是否存在
     * @param email 邮箱参数
     * @return  返回用户名可用/用户名已存在
     */
    @GetMapping("/ckusr")
    public ResultUtil checkUser(@RequestParam("name") String email){
        //1. null校验 2. "" 校验 --email == null || "".equals(email) --   3. 邮箱格式的校验
        if (StringUtils.isEmpty(email) || !validEmail(email)){
            return ResultUtil.failed(ItripUserErrorCodeEnum.INVALID_PARAM.getStatusMessage(),
                    ItripUserErrorCodeEnum.INVALID_PARAM.getStatusCode());
        }
        try {
            ItripUser itripUser = itripUserService.queryItripUserByEmail(email);
            if (itripUser != null){
                return ResultUtil.failed(ItripUserErrorCodeEnum.USER_EXISTED.getStatusMessage(),
                        ItripUserErrorCodeEnum.USER_EXISTED.getStatusCode());
            }else {
                return ResultUtil.success(ItripUserErrorCodeEnum.USER_CAN_USING.getStatusMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed(ItripUserErrorCodeEnum.QUERY_EXCEPTIONN.getStatusMessage(),
                    ItripUserErrorCodeEnum.QUERY_EXCEPTIONN.getStatusCode());
        }
    }

    /**
     *  注册新用户
     * @param itripUserVO
     * @return
     */
    @PostMapping("/doregister")
    public ResultUtil addUser(@RequestBody ItripUserVO itripUserVO){
        String userCode = itripUserVO.getUserCode();
        //验证前端传来的参数
        if (itripUserVO == null || StringUtils.isEmpty(userCode) ||
                StringUtils.isEmpty(itripUserVO.getUserPassword()) || !validEmail(userCode)){
            return ResultUtil.failed(ItripUserErrorCodeEnum.INVALID_PARAM.getStatusMessage(),
                    ItripUserErrorCodeEnum.INVALID_PARAM.getStatusCode());
        }
//        checkUser("1051@qq.com");
        try {
            ItripUser itripUser = itripUserService.queryItripUserByEmail(itripUserVO.getUserCode());
            if(itripUser==null){
                itripUserService.addItripUserInfo(itripUserVO);
                return ResultUtil.success(ItripUserErrorCodeEnum.REGISTER_SUCCESS.getStatusMessage());
            }else{
                return ResultUtil.failed(ItripUserErrorCodeEnum.REGISTER_FAILED.getStatusMessage(),
                        ItripUserErrorCodeEnum.REGISTER_FAILED.getStatusCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed(ItripUserErrorCodeEnum.ADD_FAILED.getStatusMessage(),
                    ItripUserErrorCodeEnum.ADD_FAILED.getStatusCode());
        }
    }

    /**
     *  激活用户
     * @param email
     * @param activatedCode
     * @return
     */
    @PutMapping("/activate")
    public ResultUtil updateUserActivated(@RequestParam("user")String email,@RequestParam("code")String activatedCode){
        try {
            boolean isActivated = itripUserService.updateItripUserActivated(email,activatedCode);
            if (isActivated){
                return ResultUtil.success(ItripUserErrorCodeEnum.REGISTER_SUCCESS.getStatusMessage());
            }else{
                return ResultUtil.failed(ItripUserErrorCodeEnum.ACTIVATE_FAILED.getStatusMessage(),
                        ItripUserErrorCodeEnum.ACTIVATE_FAILED.getStatusCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed(ItripUserErrorCodeEnum.ACTIVATE_ERROR.getStatusMessage(),
                    ItripUserErrorCodeEnum.ACTIVATE_ERROR.getStatusCode());
        }
    }








    /**			 *
     * 合法E-mail地址：
     * 1. 必须包含一个并且只有一个符号“@”
     * 2. 第一个字符不得是“@”或者“.”
     * 3. 不允许出现“@.”或者.@
     * 4. 结尾不得是字符“@”或者“.”
     * 5. 允许“@”前的字符中出现“＋”
     * 6. 不允许“＋”在最前面，或者“＋@”
     */
    private static boolean validEmail(String email){
        String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"  ;
        return Pattern.compile(regex).matcher(email).find();
    }
    public static void main(String[] args) {
        boolean flag = RegisterController.validEmail("1044732267@qq.com");
        System.out.println(flag);
    }
}


